**************************************************************
***** Name:        do_analysis_team.do      		     *****
***** Description: This do file analyzes data at the	 *****
***** team level.										 *****
**************************************************************

clear all
global path "C:\Users\patry\OneDrive\Desktop\Mario replication files"
cd "$path"
set more off

use "Data/clean data/clean_team.dta", clear

global controls_p2 "round_diff z_game_mean z_game_min z_game_max"
global controls_p3 $controls_p2 phase2score 
global myopts "bf(a2) sfmt(a2) r2 se label prehead(\begin{tabular}{l*{@M}{c}}) postfoot(\end{tabular}) mlabels(, prefix() suffix() depvars) noisily noeqlines nonumbers varlabels(_cons Constant, end("" ) nolast)  starlevels(* 0.1 ** 0.05 *** 0.01)"



****************************************************
****************************************************
****************************************************
**********        MAIN TEXT TABLES 	      ********** 
****************************************************
****************************************************
****************************************************


****************************************************
*	TABLE 1B: SUMMARY STATISTICS (TEAM-LEVEL) 	   *
****************************************************

 
estpost sum totalingred score cfail round_diff  z_game_mean if sample==1, d
est store a 
esttab a using Output/Tables/Table1B.tex,  replace cells("mean (label(Mean) fmt(2)) sd (label(Std.Dev) fmt(2))  min (label(Min) fmt(2)) p25 (label(P25) fmt(2)) p50 (label(Median) fmt(2)) p75 (label(P75) fmt(2)) max (label(Max) fmt(2)) count (label(N) fmt(0))")   noobs nomtitle nonumber varlabels(`e(labels)') varwidth(20) 
 

****************************************************
*	TABLE 2: EFFECT OF AI AND NEW HIRES ON INGRED  *
****************************************************


qui reg totalingred  ai newhire tacit $controls_p3 i.round if sample==1 & phase3==1, r cluster(team_id)
	estimates store t_p3
	qui sum totalingred if ai==0 & newhire==0 & tacit==0 & phase3==1
	estadd scalar ctrlmean = r(mean)
	estadd local rounds "Phase 3"
	qui test ai = newhire
	estadd scalar p = r(p)
qui reg totalingred  ai newhire tacit $controls_p3 i.round if sample==1 & round==7, r cluster(team_id)
	estimates store t_r7
	qui sum totalingred if ai==0 & newhire==0 & tacit==0 & round==7
	estadd scalar ctrlmean = r(mean)
	estadd local rounds "7"
	qui test ai = newhire
	estadd scalar p = r(p)
qui reg totalingred  ai newhire tacit $controls_p3 i.round if sample==1 & inrange(round,8,11), r cluster(team_id)
	estimates store t_r811
	qui sum totalingred if ai==0 & newhire==0 & tacit==0 &  inrange(round,8,11)
	estadd scalar ctrlmean = r(mean)
	estadd local rounds "8-11"
	qui test ai = newhire
	estadd scalar p = r(p)
qui reg totalingred  ai newhire tacit $controls_p3 i.round if sample==1 & round==12, r cluster(team_id)
	estimates store t_r12
	qui sum totalingred if ai==0 & newhire==0 & tacit==0 & round==12
	estadd scalar ctrlmean = r(mean)
	estadd local rounds "12"
	qui test ai = newhire
	estadd scalar p = r(p)
	
qui reg totalingred  ai_direct ai_spillover newhire_direct newhire_spillover tacit $controls_p3 i.round if sample==1 & phase3==1, r cluster(team_id)
	estimates store ds_p3
	qui sum totalingred if ai==0 & newhire==0 & tacit==0 & phase3==1
	estadd scalar ctrlmean = r(mean)
	estadd local rounds "Phase 3"
	qui test ai_direct = newhire_direct
	estadd scalar p_d = r(p)
	qui test ai_spillover = newhire_spillover
	estadd scalar p_s = r(p)
qui reg totalingred  ai_direct ai_spillover newhire_direct newhire_spillover tacit $controls_p3 i.round if sample==1 & round==7, r cluster(team_id)
	estimates store ds_r7
	qui sum totalingred if ai==0 & newhire==0 & tacit==0 & round==7
	estadd scalar ctrlmean = r(mean)
	estadd local rounds "7"
	qui test ai_direct = newhire_direct
	estadd scalar p_d = r(p)
	qui test ai_spillover = newhire_spillover
	estadd scalar p_s = r(p)
qui reg totalingred  ai_direct ai_spillover newhire_direct newhire_spillover tacit $controls_p3 i.round if sample==1 & inrange(round,8,11), r cluster(team_id)
	estimates store ds_r811
	qui sum totalingred if ai==0 & newhire==0 & tacit==0 &  inrange(round,8,11)
	estadd scalar ctrlmean = r(mean)
	estadd local rounds "8-11"
	qui test ai_direct = newhire_direct
	estadd scalar p_d = r(p)
	qui test ai_spillover = newhire_spillover
	estadd scalar p_s = r(p)
qui reg totalingred  ai_direct ai_spillover newhire_direct newhire_spillover tacit $controls_p3 i.round if sample==1 & round==12, r cluster(team_id)
	estimates store ds_r12	
	qui sum totalingred if ai==0 & newhire==0 & tacit==0 & round==12
	estadd scalar ctrlmean = r(mean)
	estadd local rounds "12"
	qui test ai_direct = newhire_direct
	estadd scalar p_d = r(p)
	qui test ai_spillover = newhire_spillover
	estadd scalar p_s = r(p)
	
esttab  t_p3 t_r7 t_r811  t_r12 ds_p3 ds_r7 ds_r811 ds_r12 using Output/Tables/Table2.tex,  $myopts keep(ai newhire ai_direct ai_spillover newhire_direct newhire_spillover)   stats(r2 N ctrlmean rounds space p p_d p_s , label("R2" "Observations" "Control mean" "Rounds" "P-values:" "AI = New Hire" "AI = New Hire, Direct" "AI = New Hire, Spillover")  fmt(%9.3f 0 %9.3f 0 %9.3f %9.3f  %9.3f))  replace


*******************************************************************
*	TABLE 3: EFFECT OF AI AND NEW HIRES ON COORDINATION FAILURES  *
*******************************************************************


qui reg cfail  ai newhire tacit $controls_p3 i.round if sample==1 & phase3==1, r cluster(team_id)
	estimates store t_p3
	qui sum cfail if ai==0 & newhire==0 & tacit==0 & phase3==1
	estadd scalar ctrlmean = r(mean)
	estadd local rounds "Phase 3"
	qui test ai = newhire
	estadd scalar p = r(p)
qui reg cfail  ai newhire tacit $controls_p3 i.round if sample==1 & round==7, r cluster(team_id)
	estimates store t_r7
	qui sum cfail if ai==0 & newhire==0 & tacit==0 & round==7
	estadd scalar ctrlmean = r(mean)
	estadd local rounds "7"
	qui test ai = newhire
	estadd scalar p = r(p)
qui reg cfail  ai newhire tacit $controls_p3 i.round if sample==1 & inrange(round,8,11), r cluster(team_id)
	estimates store t_r811
	qui sum cfail if ai==0 & newhire==0 & tacit==0 &  inrange(round,8,11)
	estadd scalar ctrlmean = r(mean)
	estadd local rounds "8-11"
	qui test ai = newhire
	estadd scalar p = r(p)
qui reg cfail  ai newhire tacit $controls_p3 i.round if sample==1 & round==12, r cluster(team_id)
	estimates store t_r12
	qui sum cfail if ai==0 & newhire==0 & tacit==0 & round==12
	estadd scalar ctrlmean = r(mean)
	estadd local rounds "12"
	qui test ai = newhire
	estadd scalar p = r(p)
	
qui reg cfail  ai_direct ai_spillover newhire_direct newhire_spillover tacit $controls_p3 i.round if sample==1 & phase3==1, r cluster(team_id)
	estimates store ds_p3
	qui sum cfail if ai==0 & newhire==0 & tacit==0 & phase3==1
	estadd scalar ctrlmean = r(mean)
	estadd local rounds "Phase 3"
	qui test ai_direct = newhire_direct
	estadd scalar p_d = r(p)
	qui test ai_spillover = newhire_spillover
	estadd scalar p_s = r(p)
qui reg cfail  ai_direct ai_spillover newhire_direct newhire_spillover tacit $controls_p3 i.round if sample==1 & round==7, r cluster(team_id)
	estimates store ds_r7
	qui sum cfail if ai==0 & newhire==0 & tacit==0 & round==7
	estadd scalar ctrlmean = r(mean)
	estadd local rounds "7"
	qui test ai_direct = newhire_direct
	estadd scalar p_d = r(p)
	qui test ai_spillover = newhire_spillover
	estadd scalar p_s = r(p)
qui reg cfail  ai_direct ai_spillover newhire_direct newhire_spillover tacit $controls_p3 i.round if sample==1 & inrange(round,8,11), r cluster(team_id)
	estimates store ds_r811
	qui sum cfail if ai==0 & newhire==0 & tacit==0 &  inrange(round,8,11)
	estadd scalar ctrlmean = r(mean)
	estadd local rounds "8-11"
	qui test ai_direct = newhire_direct
	estadd scalar p_d = r(p)
	qui test ai_spillover = newhire_spillover
	estadd scalar p_s = r(p)
qui reg cfail  ai_direct ai_spillover newhire_direct newhire_spillover tacit $controls_p3 i.round if sample==1 & round==12, r cluster(team_id)
	estimates store ds_r12	
	qui sum cfail if ai==0 & newhire==0 & tacit==0 & round==12
	estadd scalar ctrlmean = r(mean)
	estadd local rounds "12"
	qui test ai_direct = newhire_direct
	estadd scalar p_d = r(p)
	qui test ai_spillover = newhire_spillover
	estadd scalar p_s = r(p)	
	
esttab  t_p3 t_r7 t_r811  t_r12 ds_p3 ds_r7 ds_r811 ds_r12 using Output/Tables/Table3.tex,  $myopts keep(ai newhire ai_direct ai_spillover newhire_direct newhire_spillover)   stats(r2 N ctrlmean rounds space p p_d p_s , label("R2" "Observations" "Control mean" "Rounds" "P-values:" "AI = New Hire" "AI = New Hire, Direct" "AI = New Hire, Spillover")  fmt(%9.3f 0 %9.3f 0 %9.3f %9.3f  %9.3f))  replace


********************************************************
*	TABLE 5A: SKILLS ANALYSIS (HETEROGENEITY BY SKILL  *
********************************************************


* REPLACED PLAYER

qui reg totalingred ai newhire tacit i.round  $controls_p3  if phase3==1 & sample==1 & z_game_p1_high==1, 
	eststo high
qui reg totalingred ai newhire tacit i.round  $controls_p3  if phase3==1 & sample==1 & z_game_p1_middle==1, 
	eststo middle
qui reg totalingred ai newhire tacit i.round  $controls_p3  if phase3==1 & sample==1 & z_game_p1_low==1, 
	eststo low
quietly: suest high middle low,  vce(cluster team_id)

	test [high_mean]ai=[middle_mean]ai = [low_mean]ai
	local ai_p = r(p)
	test [high_mean]newhire=[middle_mean]newhire = [low_mean]newhire
	local newhire_p = r(p)

qui reg totalingred ai newhire tacit i.round  $controls_p3  if phase3==1 & sample==1 & z_game_p1_high==1, r cluster(team_id)
  estimates store high_exit
	estadd local rounds "7-12"
	estadd local group "High"
qui reg totalingred ai newhire tacit i.round  $controls_p3  if phase3==1 & sample==1 & z_game_p1_middle==1, r cluster(team_id)
  estimates store middle_exit
	estadd local rounds "7-12"
	estadd local group "Middle"
	estadd local ai_p `=round(`ai_p',.001)'
	estadd local newhire_p `=round(`newhire_p',.001)'
qui reg totalingred ai newhire tacit i.round  $controls_p3  if phase3==1 & sample==1 & z_game_p1_low==1, r cluster(team_id)
  estimates store low_exit
	estadd local rounds "7-12"
	estadd local group "Low"


* REMAINING PLAYERS

qui reg totalingred ai newhire tacit i.round  $controls_p3  if phase3==1 & sample==1 & z_game_p234_high==1, 
	eststo high
qui reg totalingred ai newhire tacit i.round  $controls_p3  if phase3==1 & sample==1 & z_game_p234_middle==1, 
	eststo middle
qui reg totalingred ai newhire tacit i.round  $controls_p3  if phase3==1 & sample==1 & z_game_p234_low==1, 
	eststo low
quietly: suest high middle low,  vce(cluster team_id)

	test [high_mean]ai=[middle_mean]ai = [low_mean]ai
	local ai_p = r(p)
	test [high_mean]newhire=[middle_mean]newhire = [low_mean]newhire
	local newhire_p = r(p)
	
qui reg totalingred ai newhire tacit i.round  $controls_p3  if phase3==1 & sample==1 & z_game_p234_high==1, r cluster(team_id)
  estimates store high_remain
	estadd local rounds "7-12"
	estadd local group "High"
	
qui reg totalingred ai newhire tacit i.round  $controls_p3  if phase3==1 & sample==1 & z_game_p234_middle==1, r cluster(team_id)
  estimates store middle_remain
	estadd local rounds "7-12"
	estadd local group "Middle"
	estadd local ai_p `=round(`ai_p',.001)'
	estadd local newhire_p `=round(`newhire_p',.001)'

qui reg totalingred ai newhire tacit i.round  $controls_p3  if phase3==1 & sample==1 & z_game_p234_low==1, r cluster(team_id)
  estimates store low_remain
	estadd local rounds "7-12"
	estadd local group "Low"

esttab  low_exit middle_exit high_exit low_remain middle_remain high_remain using Output/Tables/Table5A.tex,  keep( ai newhire) se starlevels( * 0.10 ** 0.05 *** 0.010) stats(r2 N group rounds s ai_p newhire_p, label("R2" "Observations" "Skill" "Rounds" "\hline P-value (Low=Middle=High)" "AI" "New Hire" )  fmt(%9.3f 0 %9.3f 0 %9.3f %9.3f ))  replace


**********************************************************************
*	TABLE 5B: SKILLS ANALYSIS (IMPACT OF SKILLS ON TEAM PERFORMANCE  *
**********************************************************************


tab team_id, gen(team_id_)
		
qui reg totalingred z_game_max z_game_min i.round ai newhire tacit if phase3==1, r cluster(team_id) 
  estimates store skill_1
  estadd local fe "No"
  
qui  reg totalingred z_game_max z_game_min i.round i.team_id_*  if phase3==1, r cluster(team_id) 
  estimates store skill_2
  estadd local fe "Yes"
 
 esttab  skill_1 skill_2 using Output/Tables/Table5B.tex,  $myopts keep(z_game_max z_game_min )   stats(r2 N fe   , label("R2" "Observations" "Firm fixed effect" )  fmt(%9.3f 0 %9.3f 0 %9.3f %9.3f  %9.3f))  replace
  

****************************************************
****************************************************
****************************************************
**********        MAIN TEXT FIGURES 	  ********** 
****************************************************
****************************************************
****************************************************


*********************************************************************
*	FIGURE 3A: CUMULATIVE PERFORMANCE, TACIT VS EXPLICIT IN CONTROL * 
*********************************************************************

 
preserve 
	 
	reg totalingred tacitXround_1-tacitXround_12  explicitXround_1-explicitXround_12 $controls_p2 if control==1 & sample==1, r cluster(team_id) nocons

	* Store treatment effects

	foreach y in tacit explicit { 
		local i = 0
		* Difference in round 1
		local ++i
			local round_`i' = `i'
			local b_`y'_`i' = _b[`y'Xround_2]
			local se_`y'_`i' = _b[`y'Xround_2]
			local cil_`y'_`i' = _b[`y'Xround_2] - invttail(e(df_r),0.025)*_se[`y'Xround_2]
			local ciu_`y'_`i' = _b[`y'Xround_2] + invttail(e(df_r),0.025)*_se[`y'Xround_2]
		* Rounds after
		local lincom "`y'Xround_2"
		forv i = 2/12 {
			local round_`i' `i'
			lincom `lincom' + `y'Xround_`i'
			local b_`y'_`i'= r(estimate)
			local se_`y'_`i' = r(se)
			local cil_`y'_`i' = r(estimate) - invttail(r(df),0.025)*r(se) 
			local ciu_`y'_`i' = r(estimate) + invttail(r(df),0.025)*r(se)
			local lincom "`lincom' + `y'Xround_`i'"
		}
	}

	clear 
	local I 12
	set obs `I'
	foreach x in round b_tacit se_tacit cil_tacit ciu_tacit b_explicit se_explicit cil_explicit ciu_explicit {
		gen `x' = ""
		forv i = 1/`I' {
			replace `x' = "``x'_`i''" in `i'
		}
		destring `x', replace
	}

	gen round_tacit = round-.1
	gen round_explicit = round+.1
		
	twoway (scatter b_tacit round_tacit, msymbol(circle) mcolor(gs0)) (rcap ciu_tacit cil_tacit  round_tacit , lcolor(gs0)) ///
		   (scatter b_explicit round_explicit, msymbol(circle) mcolor(gs8)) (rcap ciu_explicit cil_explicit  round_explicit , lcolor(gs8)), ///
				 xtitle("Round") ytitle("Cumulative ingredients returned") graphregion(color(white)) bgcolor(white)   ///
		xscale(range(1 12 )) xlabel(1(1)12) yscale(range(0 160 )) ylabel(0(40)160)  legend(order( 1 "Tacit" 3 "Explicit"))

	graph export "Output/Figures/Figure3a.pdf", replace

restore


******************************************************************
*	FIGURE 3B: CUMULATIVE PERFORMANCE, AI VS NEW HIRE VS CONTROL * 
******************************************************************


preserve

	qui reg totalingred aiXround_1-aiXround_6  newhireXround_1-newhireXround_6  i.round $controls_p2 if phase2==1 & sample==1, r cluster(team_id)
	* Store treatment effects
	foreach y in ai newhire { 
		forv i = 1/6 { 
		* Difference in phase 2
			local round_`i' = `i'
			local b_`y'_`i' = _b[`y'Xround_`i']
			local se_`y'_`i' = _b[`y'Xround_`i']
			local cil_`y'_`i' = _b[`y'Xround_`i'] - invttail(e(df_r),0.025)*_se[`y'Xround_`i']
			local ciu_`y'_`i' = _b[`y'Xround_`i'] + invttail(e(df_r),0.025)*_se[`y'Xround_`i']
		}
	}
	qui reg totalingred aiXround_7-aiXround_12  newhireXround_7-newhireXround_12  i.round $controls_p3 if phase3==1 & sample==1, r cluster(team_id)
	* Round 7
	local i 7 
	foreach y in ai newhire { 
			local round_`i' = `i'
			local b_`y'_`i' = _b[`y'Xround_`i']
			local se_`y'_`i' = _b[`y'Xround_`i']
			local cil_`y'_`i' = _b[`y'Xround_`i'] - invttail(e(df_r),0.025)*_se[`y'Xround_`i']
			local ciu_`y'_`i' = _b[`y'Xround_`i'] + invttail(e(df_r),0.025)*_se[`y'Xround_`i']
	}
	* Rounds 8-12
	foreach y in ai newhire { 
		local lincom "`y'Xround_7"
		forv i = 8/12 {
			local round_`i' `i'
			qui lincom `lincom' + `y'Xround_`i'
			local b_`y'_`i'= r(estimate)
			local se_`y'_`i' = r(se)
			local cil_`y'_`i' = r(estimate) - invttail(r(df),0.025)*r(se) 
			local ciu_`y'_`i' = r(estimate) + invttail(r(df),0.025)*r(se)
			local lincom "`lincom' + `y'Xround_`i'"
		}
	}

	clear 
	local I 12
	set obs `I'
	foreach x in round b_ai se_ai cil_ai ciu_ai b_newhire se_newhire cil_newhire ciu_newhire  {
		gen `x' = ""
		forv i = 1/`I' {
			replace `x' = "``x'_`i''" in `i'
		}
		destring `x', replace
	}

	gen round_ai = round-.1
	gen round_newhire = round+.1
	
	twoway (scatter b_ai round_ai, msymbol(circle) mcolor(gs0)) (rcap ciu_ai cil_ai  round_ai , lcolor(gs0)) ///
		   (scatter b_newhire round_newhire, msymbol(circle) mcolor(gs8)) (rcap ciu_newhire cil_newhire  round_newhire , lcolor(gs8)), ///
				 xtitle("Round") ytitle("Change in cumulative ingredients returned") graphregion(color(white)) bgcolor(white)   ///
		xscale(range(1 12 )) xlabel(1(1)12) xline(6.5, lcolor(gs10) lpattern(dash)) yline(0, lcolor(black))  legend(order( 1 "AI" 3 "New Hire"))
	graph export "Output/Figures/Figure3B.pdf", replace

restore

  
****************************************************
****************************************************
****************************************************
**********       APPENDIX TABLES 	      ********** 
****************************************************
****************************************************
****************************************************


**********************************************************************
*	   TABLE B1: EFFECT OF TACIT MANIPULATION ON TEAM PERFORMANCE    *
**********************************************************************


* all organizational change
qui reg totalingred  tacit ai newhire i.round  $controls_p2  if sample==1, r cluster(team_id)
  estimates store all12
  qui sum totalingred if ai==0 & newhire==0 & explicit==0 
	estadd scalar ctrlmean = r(mean)
	estadd local rounds "1-12"
qui reg totalingred  tacit ai newhire i.round  $controls_p2  if sample==1 & phase2==1, r cluster(team_id)
  estimates store p2
  qui sum totalingred if ai==0 & newhire==0 & explicit==0 & phase2==1
	estadd scalar ctrlmean = r(mean)
	estadd local rounds "Phase 2"
qui reg totalingred  tacit i.round  $controls_p3  if sample==1 & phase3==1, r cluster(team_id)
  estimates store p3
  qui sum totalingred if ai==0 & newhire==0 & explicit==0 & phase3==1
	estadd scalar ctrlmean = r(mean)
	estadd local rounds "Phase 3"
* control group only 
qui reg totalingred  tacit i.round  $controls_p2  if sample==1 & control==1, r cluster(team_id)
  estimates store all12_c
  qui sum totalingred if ai==0 & newhire==0 & explicit==0 & control==1
	estadd scalar ctrlmean = r(mean)
	estadd local rounds "1-12"
qui reg totalingred  tacit i.round  $controls_p2  if sample==1 & phase2==1 & control==1, r cluster(team_id)
  estimates store p2_c
  qui sum totalingred if ai==0 & newhire==0 & explicit==0 & phase2==1 & control==1
	estadd scalar ctrlmean = r(mean)
	estadd local rounds "Phase 2"
qui reg totalingred  tacit i.round  $controls_p3  if sample==1 & phase3==1 & control==1 , r cluster(team_id)
  estimates store p3_c
  qui sum totalingred if ai==0 & newhire==0 & explicit==0 & phase3==1 & control==1
	estadd scalar ctrlmean = r(mean)
	estadd local rounds "Phase 3"	
  
esttab  all12 p2 p3 all12_c p2_c p3_c using Output/Tables/Appendix/TableB1.tex,  keep( tacit) se starlevels( * 0.10 ** 0.05 *** 0.010) stats(r2 N ctrlmean rounds , label("R2" "Observations" "Control mean" "Rounds" )  fmt(%9.3f 0 %9.3f 0 %9.3f %9.3f ))  replace
	
	
**********************************************************************
*	   TABLE B3: HETEROGENEITY BY TEAM STRUCTURE AND TASK DIFFICULTY *
**********************************************************************

	
gen aiXround_diff = ai * round_diff
gen newhireXround_diff = newhire * round_diff
	
qui reg totalingred  ai aiXtacit newhire newhireXtacit tacit i.round  $controls_p3 if phase3==1 & sample==1, r cluster(team_id)
  estimates store tacit
	qui sum totalingred if ai==0 & newhire==0 & phase3==1 
	estadd scalar ctrlmean = r(mean)
	estadd local rounds "7-12"
qui reg totalingred  ai aiXround_diff newhire newhireXround_diff round_diff tacit i.round  $controls_p3 if phase3==1 & sample==1, r cluster(team_id)
  estimates store diff
	qui sum cfail if ai==0 & newhire==0 & phase3==1 
	estadd scalar ctrlmean = r(mean)
	estadd local rounds "7-12"

esttab  tacit diff using Output/Tables/Appendix/TableB3.tex,  $myopts keep(ai aiXtacit newhire newhireXtacit ai aiXround_diff newhire newhireXround_diff )   stats(r2 N ctrlmean rounds  , label("R2" "Observations" "Control mean" )  fmt(%9.3f 0 %9.3f 0 %9.3f %9.3f  %9.3f))  replace
	
	
**********************************************************************
*	   TABLE B4: CORRELATION B/W COORDINATION AND RECIPES 			 *
**********************************************************************


qui reg totalingred cfail  if sample==1 , r cluster(team_id) 
	 estimates store t1
	 estadd local roundfe "No"
	 estadd local treatment "No"
	 estadd local teamfe "No"
qui reg totalingred cfail  i.round if sample==1  , r cluster(team_id) 
	 estimates store t2
	 estadd local roundfe "Yes"
	 estadd local treatment "No"
	 estadd local teamfe "No"
qui reg totalingred cfail  i.round tacit ai newhire if sample==1 , r cluster(team_id) 
	 estimates store t3
	 estadd local roundfe "Yes"
	 estadd local treatment "Yes"
	 estadd local teamfe "No"
qui areg totalingred cfail   i.round if sample==1 , r cluster(team_id) absorb(team_id)
	 estimates store t4
	 estadd local roundfe "Yes"
	 estadd local treatment "No"
	 estadd local teamfe "Yes"

esttab t1 t2 t3 t4 using Output/Tables/Appendix/TableB4.tex,  keep(cfail) se starlevels( * 0.10 ** 0.05 *** 0.010) stats(r2 N roundfe treatment teamfe , label("R2" "Observations" "Round f.e" "Treatment" "Team f.e.")  fmt(%9.3f 0 %9.3f 0 %9.3f %9.3f ))  replace
 

**********************************************************************
*	   TABLE B11: CORRELATION B/W TEAM SKILLS AND TEAM INGREDIENTS   *
**********************************************************************


qui reg totalingred skills_mean if sample==1, r cluster(team_id) 
	 estimates store t1
	 estadd local roundfe "No"
	 estadd local treatment "No"
	 estadd local teamfe "No"
qui reg totalingred skills_mean i.round if sample==1 , r cluster(team_id) 
	 estimates store t2
	 estadd local roundfe "Yes"
	 estadd local treatment "No"
	 estadd local teamfe "No"
qui reg totalingred skills_mean i.round tacit ai newhire if sample==1, r cluster(team_id) 
	 estimates store t3
	 estadd local roundfe "Yes"
	 estadd local treatment "Yes"
	 estadd local teamfe "No"
qui areg totalingred skills_mean  i.round if sample==1, r cluster(team_id) absorb(team_id)
	 estimates store t4
	 estadd local roundfe "Yes"
	 estadd local treatment "No"
	 estadd local teamfe "Yes"
 
 esttab t1 t2 t3 t4 using Output/Tables/Appendix/TableB11.tex,  keep(skills_mean) se starlevels( * 0.10 ** 0.05 *** 0.010) stats(r2 N roundfe treatment teamfe , label("R2" "Observations" "Round f.e" "Treatment" "Team f.e.")  fmt(%9.3f 0 %9.3f 0 %9.3f %9.3f ))  replace

 
**********************************************************************************
*	   TABLE B14: IMPACT OF PARTNER ASSIGNMENT ON HIGH SKILL TEAMS COORDINATION  *
**********************************************************************************
	
	
qui reg cfail ai   if z_game_p234_high==1 &  newhire==0 & phase3==1, r cluster(team_id)
    estimates store highskill1
    estadd local roundctrl "No"
qui reg cfail ai i.round round_diff  if z_game_p234_high==1 &  newhire==0 & phase3==1, r cluster(team_id)
	estimates store highskill2 
    estadd local roundctrl "Yes"
esttab highskill1 highskill2 using Output/Tables/Appendix/TableB14.tex,  keep(ai) se starlevels( * 0.10 ** 0.05 *** 0.010) stats(r2 N roundctrl , label("R2" "Observations" "Round controls")  fmt(%9.3f 0 %9.3f 0 %9.3f %9.3f ))  replace
  
  
**************************************************************************
*	   TABLE B15: HETEROGENEITY IN DIRECT AND SPILLOVER EFFECTS BY TEAM  *
**************************************************************************


qui reg totalingred ai_direct ai_spillover newhire_direct newhire_spillover tacit i.round  $controls_p3  if phase3==1 & sample==1 & z_game_p234_high==1, 
	eststo high
qui reg totalingred ai_direct ai_spillover newhire_direct newhire_spillover  tacit i.round  $controls_p3  if phase3==1 & sample==1 & z_game_p234_middle==1, 
	eststo middle
qui reg totalingred ai_direct ai_spillover newhire_direct newhire_spillover  tacit i.round  $controls_p3  if phase3==1 & sample==1 & z_game_p234_low==1, 
	eststo low
quietly: suest high middle low,  vce(cluster team_id)

	test [high_mean]ai_direct=[middle_mean]ai_direct = [low_mean]ai_direct
	local ai_d_p = r(p)
	test [high_mean]ai_spillover=[middle_mean]ai_spillover = [low_mean]ai_spillover
	local ai_s_p = r(p)
	test [high_mean]newhire_direct=[middle_mean]newhire_direct = [low_mean]newhire_direct
	local newhire_d_p = r(p)
	test [high_mean]newhire_spillover=[middle_mean]newhire_spillover = [low_mean]newhire_spillover
	local newhire_s_p = r(p)
	
* Run regressionn
qui reg totalingred ai_direct ai_spillover newhire_direct newhire_spillover tacit i.round  $controls_p3  if phase3==1 & sample==1 & z_game_p234_high==1, r cluster(team_id)
  estimates store high_remain
	estadd local rounds "7-12"
	estadd local group "High"
	
qui reg totalingred ai_direct ai_spillover newhire_direct newhire_spillover tacit i.round  $controls_p3  if phase3==1 & sample==1 & z_game_p234_middle==1, r cluster(team_id)
  estimates store middle_remain
	estadd local rounds "7-12"
	estadd local group "Middle"
	estadd local ai_p `=round(`ai_p',.001)'
	estadd local newhire_p `=round(`newhire_p',.001)'

qui reg totalingred ai_direct ai_spillover newhire_direct newhire_spillover tacit i.round  $controls_p3  if phase3==1 & sample==1 & z_game_p234_low==1, r cluster(team_id)
  estimates store low_remain
	estadd local rounds "7-12"
	estadd local group "Low"

esttab  low_remain middle_remain high_remain using Output/Tables/Appendix/TableB15.tex,  keep( ai_direct ai_spillover newhire_direct newhire_spillover) se starlevels( * 0.10 ** 0.05 *** 0.010) stats(r2 N group rounds s ai_p newhire_p, label("R2" "Observations" "Skill" "Rounds" "\hline P-value (Low=Middle=High)" "AI" "New Hire" )  fmt(%9.3f 0 %9.3f 0 %9.3f %9.3f ))  replace
		

*************************************************************************
*	   TABLE B17: CORRELATION B/W ROUND DIFFICULTY AND TEAM PERFORMANCE *
*************************************************************************

qui reg totalingred round_diff, r cluster(team_id) 
	 estimates store t1
	 estadd local roundfe "No"
	 estadd local treatment "No"
	 estadd local teamfe "No"
qui reg totalingred round_diff i.round, r cluster(team_id) 
	 estimates store t2
	 estadd local roundfe "Yes"
	 estadd local treatment "No"
	 estadd local teamfe "No"
qui reg totalingred round_diff i.round tacit ai newhire, r cluster(team_id) 
	 estimates store t3
	 estadd local roundfe "Yes"
	 estadd local treatment "Yes"
	 estadd local teamfe "No"
qui reg totalingred round_diff team_id_* i.round, r cluster(team_id)
	 estimates store t4
	 estadd local roundfe "Yes"
	 estadd local treatment "No"
	 estadd local teamfe "Yes"
 
esttab t1 t2 t3 t4 using Output/Tables/Appendix/TableB17.tex,  keep(round_diff) se starlevels( * 0.10 ** 0.05 *** 0.010) stats(r2 N roundfe treatment teamfe , label("R2" "Observations" "Round f.e" "Treatment" "Team f.e.")  fmt(%9.3f 0 %9.3f 0 %9.3f %9.3f ))  replace

  
****************************************************
****************************************************
****************************************************
**********       APPENDIX FIGURES	      ********** 
****************************************************
****************************************************
****************************************************


*************************************************************************
*	   FIGURE B.5: AGGREGATE PERFORMANCE PLOT, DIRECTLY-IMPACTED TEAMS  *
*	   FIGURE B.6: AGGREGATE PERFORMANCE PLOT, SPILLOVER TEAMS          *
*************************************************************************

preserve

	reg totalingred ai_directXround_1-ai_directXround_6 ai_spilloverXround_1-ai_spilloverXround_6 newhire_directXround_1-newhire_directXround_6 newhire_spilloverXround_1-newhire_spilloverXround_6 i.round $controls_p2 if phase2==1 & sample==1, r cluster(team_id)
	* Store treatment effects
	foreach y in ai_direct ai_spillover newhire_direct newhire_spillover { 
		forv i = 1/6 { 
		* Difference in phase 2
			local round_`i' = `i'
			local b_`y'_`i' = _b[`y'Xround_`i']
			local se_`y'_`i' = _b[`y'Xround_`i']
			local cil_`y'_`i' = _b[`y'Xround_`i'] - invttail(e(df_r),0.025)*_se[`y'Xround_`i']
			local ciu_`y'_`i' = _b[`y'Xround_`i'] + invttail(e(df_r),0.025)*_se[`y'Xround_`i']
		}
	}
	reg totalingred ai_directXround_7-ai_directXround_12 ai_spilloverXround_7-ai_spilloverXround_12 newhire_directXround_7-newhire_directXround_12 newhire_spilloverXround_7-newhire_spilloverXround_12 i.round $controls_p3 if phase3==1 & sample==1, r cluster(team_id)
	* Round 7
	local i 7 
	foreach y in ai_direct ai_spillover newhire_direct newhire_spillover { 
	 
			local round_`i' = `i'
			local b_`y'_`i' = _b[`y'Xround_`i']
			local se_`y'_`i' = _b[`y'Xround_`i']
			local cil_`y'_`i' = _b[`y'Xround_`i'] - invttail(e(df_r),0.025)*_se[`y'Xround_`i']
			local ciu_`y'_`i' = _b[`y'Xround_`i'] + invttail(e(df_r),0.025)*_se[`y'Xround_`i']
	}
	* Rounds 8-12
	foreach y in ai_direct ai_spillover newhire_direct newhire_spillover { 
		local lincom "`y'Xround_7"
		forv i = 8/12 {
			local round_`i' `i'
			lincom `lincom' + `y'Xround_`i'
			local b_`y'_`i'= r(estimate)
			local se_`y'_`i' = r(se)
			local cil_`y'_`i' = r(estimate) - invttail(r(df),0.025)*r(se) 
			local ciu_`y'_`i' = r(estimate) + invttail(r(df),0.025)*r(se)
			local lincom "`lincom' + `y'Xround_`i'"
		}
	}
	clear 
	local I 12
	set obs `I'
	foreach x in round { 
		gen `x' = ""
		forv i = 1/`I' {
			replace `x' = "``x'_`i''" in `i'
		}
		destring `x', replace
	}
	foreach y in ai_direct ai_spillover newhire_direct newhire_spillover { 

		foreach x in b se cil ciu { 
			gen `x'_`y' = ""
			forv i = 1/`I' {
				replace `x'_`y' = "``x'_`y'_`i''" in `i'
			}
			destring `x'_`y', replace
		}
	}
	gen round_ai = round-.1
	gen round_newhire = round+.1

		* direct effect
	twoway (scatter b_ai_direct round_ai, msymbol(circle) mcolor(blue)) (rcap ciu_ai_direct cil_ai_direct  round_ai , lcolor(black)) ///
		   (scatter b_newhire_direct round_newhire, msymbol(circle) mcolor(green)) (rcap ciu_newhire_direct cil_newhire_direct  round_newhire , lcolor(black)), ///
				 xtitle("Round") ytitle("Change in aggregate ingredients returned") graphregion(color(white)) bgcolor(white)   ///
		xscale(range(1 12 )) xlabel(1(1)12) xline(6.5, lcolor(red)) yline(0, lcolor(black))  legend(order( 1 "AI" 3 "New Hire"))
	graph export "Output/Figures/Appendix/FigureB5.pdf", replace

		* spillover effect
	twoway (scatter b_ai_spillover round_ai, msymbol(circle) mcolor(blue)) (rcap ciu_ai_spillover cil_ai_spillover  round_ai , lcolor(black)) ///
		   (scatter b_newhire_spillover round_newhire, msymbol(circle) mcolor(green)) (rcap ciu_newhire_spillover cil_newhire_spillover  round_newhire , lcolor(black)), ///
				 xtitle("Round") ytitle("Change in aggregate ingredients returned") graphregion(color(white)) bgcolor(white)   ///
		xscale(range(1 12 )) xlabel(1(1)12) xline(6.5, lcolor(red)) yline(0, lcolor(black))  legend(order( 1 "AI" 3 "New Hire"))
	graph export "Output/Figures/Appendix/FigureB6.pdf", replace

restore


*****************************************************
*	   FIGURE B.9: SKILL HETEROGENEITY BY ROUND     *
*****************************************************
	
	
preserve

	forv r=7/12 {
		eststo clear
	 qui reg totalingred ai newhire tacit i.round  $controls_p3  if phase3==1 & sample==1 & z_game_p234_high==1 & inrange(round,7,`r'), 
		 local ai_high_`r' = _b[ai]
		 local nh_high_`r' = _b[newhire]
	 qui reg totalingred ai newhire tacit i.round  $controls_p3  if phase3==1 & sample==1 & z_game_p234_high==1 & inrange(round,7,`r'), 
		eststo high
	 qui reg totalingred ai newhire tacit i.round  $controls_p3  if phase3==1 & sample==1 & z_game_p234_middle==1 & inrange(round,7,`r'), 
		eststo middle
	 qui reg totalingred ai newhire tacit i.round  $controls_p3  if phase3==1 & sample==1 & z_game_p234_middle==1 & inrange(round,7,`r'), 
		 local ai_mid_`r' = _b[ai]
		 local nh_mid_`r' = _b[newhire]
	 
	 qui reg totalingred ai newhire tacit i.round  $controls_p3  if phase3==1 & sample==1 & z_game_p234_low==1 & inrange(round,7,`r'),
		local ai_low_`r' = _b[ai]
		local nh_low_`r' = _b[newhire]
	 qui reg totalingred ai newhire tacit i.round  $controls_p3  if phase3==1 & sample==1 & z_game_p234_low==1 & inrange(round,7,`r'), 
		eststo low
		
		suest high middle low,  vce(cluster team_id)

		test [high_mean]ai=[middle_mean]ai = [low_mean]ai
		local ai_p_`r' = r(p)
		test [high_mean]newhire=[middle_mean]newhire = [low_mean]newhire
		local nh_p_`r' = r(p)

	}
	clear
	set obs 6
	gen round = _n+6
	foreach x in ai_high ai_mid ai_low nh_high nh_mid nh_low ai_p nh_p {
		qui gen `x' = ""
		forv i = 7/12 { 
			qui replace `x' = "``x'_`i''" if round==`i'
		}
		destring `x', replace
	}
		
	twoway (scatter ai_high round ) (scatter ai_mid round) (scatter ai_low round),  xtitle("Round") ytitle("Treatment effect") graphregion(color(white)) bgcolor(white)   ///
		xscale(range(7 12 )) xlabel(7(1)12)  yline(0, lcolor(black))  legend(order( 1 "High skill"  2 "Middle skill" 3 "Low skill") rows(1))
		graph export "Output/Figures/Appendix/FigureB9.pdf", replace
	
restore
	
	
*******************************************	
*******************************************
*******************************************
*	   END OF TEAM TABLES AND FIGURES     *
*******************************************
*******************************************
*******************************************
